package com.sciencep.pro.common.config;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author pxy14077
 * @date 2018/3/26
 */
@Slf4j
public final class DictionaryConstant {
    private static DictionaryConstant instance;

    private Map<String, Map<Object, String>> dicMap = new HashMap<>();

    private DictionaryConstant() {
        init();
    }

    public static DictionaryConstant getInstance() {
        if (instance == null) {
            synchronized (DictionaryConstant.class) {
                instance = new DictionaryConstant();
            }
        }
        return instance;
    }

    public static String getNameForValue(String enumName, Object value) {
        String res = "";
        try {
            res = getInstance().getDic(enumName).get(Integer.valueOf(value.toString()));
        } catch (Exception e) {
            log.error("DictionaryConstant.getNameForValue {}", e.getMessage());
        }
        return res;
    }

    private void init() {

        initErrorCode();//错误码

    }

    private void initErrorCode() {
        Map<Object, String> map = new LinkedHashMap<>();
        map.put(Constant.ErrorCode.SUCCESS, "成功");
        map.put(Constant.ErrorCode.SYSTEM_ERROR, "系统出错");
        map.put(Constant.ErrorCode.VALIDATE_ERROR, "验证错误");
        map.put(Constant.ErrorCode.SERVICE_ERROR, "业务出错");
        map.put(Constant.ErrorCode.UNAUTHORIZED_ERROR, "未认证，请在前端系统进行认证");

        this.putDic("errorCode", map);
    }


    public Map<Object, String> getDic(String dicId) {
        if (!dicMap.containsKey(dicId)) {
            return new HashMap<>();
        }
        Map<Object, String> map = new LinkedHashMap<>();
        map.putAll(dicMap.get(dicId));

        return map;
    }


    public Map<String, Map<Object, String>> getDicMap() {
        return dicMap;
    }

    private synchronized void putDic(String dicId, Map<Object, String> dataMap) {
        dicMap.remove(dicId);
        dicMap.put(dicId, dataMap);
    }
}
